/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.skywalking.apm.commons.datacarrier;

import org.apache.skywalking.apm.commons.datacarrier.buffer.*;
import org.apache.skywalking.apm.commons.datacarrier.consumer.*;
import org.apache.skywalking.apm.commons.datacarrier.partition.*;

/**
 * DataCarrier main class. use this instance to set Producer/Consumer Model.
 *
 * DataCarrier 是一个轻量级的生产者-消费者模式的实现库，SkyWalking Agent 在收集到 Trace 数据之后，
 * 会先写入到 DataCarrier 中的缓存，然后由后台线程定时发送到后端的 OAP。其实，在多数涉及网络传输的场
 * 景中都会使用这种设计：先在本地缓存数据，然后聚合，最后定时批量发送。
 *
 * DataCarrier 底层使用多个定长数组作为存储缓冲区，即 apm-datacarrier 模块中的 Buffer 类，其
 * 底层的 buffer 字段（Object[] 类型）是真正存储数据的地方。每个 Buffer 内部维护了一个环形指针
 * （AtomicRangeInteger 类型），我们可以指定其中的 value 字段（AtomicInteger 类型）从start值
 * 开始递增，当 value 递增到 end 值（int 类型）时，value 字段会被重置为 start 值，实现环形指针
 * 的效果，这样，Buffer 就可以实现循环覆盖写入的模式了。
 *
 * DataCarrier 是整个 DataCarrier 模块最顶层的门面类，其中整合 Channels、IDriver 并给 Producer 提供了一个统一的入口。
 */
public class DataCarrier<T> {
    private final int bufferSize;
    private final int channelSize;
    private Channels<T> channels; // 保存消费数据，分区策略、保存策略
    private IDriver driver; // 管理消费线程，持有消费逻辑
    private String name;

    public DataCarrier(int channelSize, int bufferSize) {
        this("DEFAULT", channelSize, bufferSize);
    }

    public DataCarrier(String name, int channelSize, int bufferSize) {
        this(name, name, channelSize, bufferSize);
    }

    public DataCarrier(String name, String envPrefix, int channelSize, int bufferSize) {
        this.name = name;
        this.bufferSize = EnvUtil.getInt(envPrefix + "_BUFFER_SIZE", bufferSize);
        this.channelSize = EnvUtil.getInt(envPrefix + "_CHANNEL_SIZE", channelSize);
        // 默认使用 SimpleRollingPartitioner 分区选择器以及 BLOCKING 策 略
        channels = new Channels<T>(channelSize, bufferSize, new SimpleRollingPartitioner<T>(), BufferStrategy.BLOCKING);
    }

    /**
     * set a new IDataPartitioner. It will cover the current one or default one.(Default is {@link
     * SimpleRollingPartitioner}
     *
     * @param dataPartitioner to partition data into different channel by some rules.
     * @return DataCarrier instance for chain
     */
    public DataCarrier setPartitioner(IDataPartitioner<T> dataPartitioner) {
        this.channels.setPartitioner(dataPartitioner);
        return this;
    }

    /**
     * override the strategy at runtime. Notice, {@link Channels} will override several channels one by one.
     *
     * @param strategy
     */
    public DataCarrier setBufferStrategy(BufferStrategy strategy) {
        this.channels.setStrategy(strategy);
        return this;
    }

    public BlockingDataCarrier<T> toBlockingDataCarrier() {
        this.channels.setStrategy(BufferStrategy.BLOCKING);
        return new BlockingDataCarrier<T>(this.channels);
    }

    /**
     * produce data to buffer, using the givven {@link BufferStrategy}.
     *
     * @param data
     * @return false means produce data failure. The data will not be consumed.
     */
    // 为生产者提供了 produce() 方法（底层调用 Channels.save() 方法），统一写入数据的入口。
    // DataCarrier 没有为生产者定义特殊的接口，上层应用直接调用其 save() 方法即可完成写入。
    // DataCarrier 消费者的具体行为都定义在 IConsumer 接口之中。
    public boolean produce(T data) {
        if (driver != null) {
            if (!driver.isRunning(channels)) {
                return false;
            }
        }

        return this.channels.save(data);
    }

    /**
     * set consumeDriver to this Carrier. consumer begin to run when {@link DataCarrier#produce} begin to work.
     *
     * @param consumerClass class of consumer
     * @param num number of consumer threads
     */
    public DataCarrier consume(Class<? extends IConsumer<T>> consumerClass, int num, long consumeCycle) {
        if (driver != null) {
            driver.close(channels);
        }
        driver = new ConsumeDriver<T>(this.name, this.channels, consumerClass, num, consumeCycle);
        driver.begin(channels);
        return this;
    }

    /**
     * set consumeDriver to this Carrier. consumer begin to run when {@link DataCarrier#produce} begin to work with 20
     * millis consume cycle.
     *
     * @param consumerClass class of consumer
     * @param num number of consumer threads
     */
    public DataCarrier consume(Class<? extends IConsumer<T>> consumerClass, int num) {
        return this.consume(consumerClass, num, 20);
    }

    /**
     * set consumeDriver to this Carrier. consumer begin to run when {@link DataCarrier#produce} begin to work.
     *
     * @param consumer single instance of consumer, all consumer threads will all use this instance.
     * @param num number of consumer threads
     * @return
     */
    // 提供了多个 consume() 方法的重载。方法重载底层是根据参数指定线程数以及轮训时间来新建 ConsumerDriver 实现消费能力的。
    public DataCarrier consume(IConsumer<T> consumer, int num, long consumeCycle) {
        if (driver != null) {
            driver.close(channels);
        }
        driver = new ConsumeDriver<T>(this.name, this.channels, consumer, num, consumeCycle);
        driver.begin(channels);
        return this;
    }

    /**
     * set consumeDriver to this Carrier. consumer begin to run when {@link DataCarrier#produce} begin to work with 20
     * millis consume cycle.
     *
     * @param consumer single instance of consumer, all consumer threads will all use this instance.
     * @param num number of consumer threads
     * @return
     */
    public DataCarrier consume(IConsumer<T> consumer, int num) {
        return this.consume(consumer, num, 20);
    }

    /**
     * Set a consumer pool to manage the channels of this DataCarrier. Then consumerPool could use its own consuming
     * model to adjust the consumer thread and throughput.
     *
     * @param consumerPool
     * @return
     */
    // 此重载则是依赖传入的 BulkConsumePool 实现数据消费能力的（注意，这里不会新建 BulkConsumePool）。
    // 这里传入的 BulkConsumePool 对象一般统一维护在 ConsumerPoolFactory 中。
    public DataCarrier consume(ConsumerPool consumerPool, IConsumer<T> consumer) {
        driver = consumerPool;
        consumerPool.add(this.name, channels, consumer);
        driver.begin(channels); // 启动消费线程
        return this;
    }

    /**
     * shutdown all consumer threads, if consumer threads are running. Notice {@link BufferStrategy}: if {@link
     * BufferStrategy} == {@link BufferStrategy#BLOCKING}, shutdown consumeDriver maybe cause blocking when producing.
     * Better way to change consumeDriver are use {@link DataCarrier#consume}
     */
    public void shutdownConsumers() {
        if (driver != null) {
            driver.close(channels);
        }
    }
}
